<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/9 0009
 * Time: 上午 11:07
 */
namespace app\admin\controller;

use app\admin\model\FreightDefault;
use app\admin\model\FreightSwitch;
use think\Exception;
use think\Request;

class Freight extends Base{
    public function index(){

        $request = $this->request;
        $param = $request->param();
        // Reset filter
        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }
        $FreightSwitch = FreightSwitch::where('id',1)->find();
        $FreightDefault = FreightDefault::where('id',1)->find();
        if(!$FreightDefault){
            $f_type = 1;
            $unified_price = 0;
            $full_amount = 0;
            $s_price = 0;
        }else{
            $f_type = $FreightDefault->f_type;
            $unified_price = $FreightDefault->unified_price;
            $full_amount = $FreightDefault->full_amount;
            $s_price = $FreightDefault->s_price;
        }
        if($request->isAjax()){
            $model = model($this->model);

            // 每页起始条数
            $start = ($param['page'] - 1) * $param['limit'];
            // 每页显示条数
            $length = $param['limit'];
            // 排序条件
            $order = '';

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }
            if($this->defaultOrder){
                $order = $this->defaultOrder;
            }

            $data = $model->where($where)->limit($start, $length)->order($order)->select();
            foreach ($data as $v){
                $v['first_weight'] = $v['first_weight']/1000;
            }
            $count = $model->where($where)->count();

            if($data){
                // 处理列表数据
                $data = $this->handleListData($data);
            }

            $result = [
                'code' => '0',
                'msg' => '',
                'data' => $data,
                'count' => $count,
            ];

            return json($result);
        }

        return $this->fetch($this->indexView,[
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param(),
            'freight_model' => $FreightSwitch->mode,
            't_type' => $f_type,
            'unified_price' => $unified_price/100,
            'full_amount' => $full_amount/100,
            's_price' => $s_price/100
        ]);
    }

    public function setFreightSwitch(Request $request){
        $param = $request->param();
        if(empty($param['mode'])){
            return $this->error('缺少参数');
        }
        FreightSwitch::where('id',1)->update(['mode'=>$param['mode']]);
        return $this->success('ok');

    }

    public function setDefauleFreight(Request $request){
        $param = $request->param();
        if(empty($param['freight_type'])){
            return $this->error('缺少参数');
        }
        if($param['freight_type'] == 1 || $param['freight_type'] == 2){
            $FreightDefault = FreightDefault::where('id',1)->find();
            if(!$FreightDefault){
                $FreightDefault = new FreightDefault();
            }
            if($param['freight_type'] == 1){
                //统一运费
                $FreightDefault->f_type = 1;
                $FreightDefault->unified_price = $param['unified_price'] * 100;
            }else{
                //满额包邮
                $FreightDefault->f_type = 2;
                $FreightDefault->full_amount = $param['full_amount'] * 100;
                $FreightDefault->s_price = $param['s_price'] * 100;
            }
            $FreightDefault->save();
            return $this->success('ok');

        }else{
            return $this->error('参数异常');
        }
    }

    public function getselectregion($id){
        if($id == 0){
            $res = \app\common\model\Freight::where('id','>',0)->select();
        }else{
            $res = \app\common\model\Freight::where('id','<>',$id)->select();
        }
        $str = '';
        if($res){
            foreach ($res as $v){
                $str .= $v['region'] . ',';
            }
        }
        return $str;
    }
    
    public function add(Request $request){
    	return $this->fetch();
    }

    public function savepost(Request $request){
        if($request->isPost()){
            try{
                $param = $request->param();
                $selectregion = $this->getselectregion($param['id']);
                if($selectregion){
                    $sele = explode(',',$selectregion);
                    $pa = explode(',',$param['regions']);
                    $res = array_intersect($sele,$pa);
                    if($res){
                        return $this->error('地区重复添加');
                    }
                }
                if($param['id'] == 0){
                    $model = new \app\common\model\Freight();
                }else{
                    $model = \app\common\model\Freight::where('id',$param['id'])->find();
                }
                $model->first_weight = $param['first_weight'] * 1000;
                $model->first_weight_price = $param['first_weight_price'] * 100;
                $model->continuous_weight_price = $param['continuous_weight_price'] * 100;
                $model->region = $param['regions'];
                $model->save();
                return $this->success('ok');
            }catch (Exception $e){
                return $this->error($e->getMessage());
            }

        }else{
            return $this->error('非法请求');
        }
    }
    
    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return $this->error('非法请求');
        }
        $res = \app\common\model\Freight::where('id',$param['id'])->find();
        $res['first_weight'] = $res['first_weight']/1000;
        $res['first_weight_price'] = $res['first_weight_price']/100;
        $res['continuous_weight_price'] = $res['continuous_weight_price']/100;
    	return $this->fetch('',[
    	    'id'=>$param['id'],
            'datainfo' => $res
        ]);
    }

    public function getdetail(){

    }
    
    public function select(){
    	return $this->fetch();
    }
}